﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using wfDesigner.Model;
using wfDesigner.ViewModel;
using Microhard.WorkflowDataAccess;


namespace wfDesigner
{
    /// <summary>
    /// AddFlowTypeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class UpdateFlowChartWindow : Window
    {
        List<MicroHard_Resources> Resourcelist = new List<MicroHard_Resources>();
        List<MicroHard_Operation> Operationlist = new List<MicroHard_Operation>();
      
        public UpdateFlowChartWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            WorkflowDBDataContext entity = new WorkflowDBDataContext();
            var typeList = entity.t_WorkFlowTypes.Select(n => new { n.TypeId, n.TypeName });

            List<FlowType> list = new List<FlowType>();

            foreach (var t in typeList)
            {
                FlowType type = new FlowType();
                type.TypeID = t.TypeId;
                type.TypeName = t.TypeName;

                list.Add(type);
            }
            this.comboBox1.ItemsSource = list;

            OugDBDataContext entity2 = new OugDBDataContext();
            Resourcelist = entity2.MicroHard_Resources.ToList();
            this.comboBox2.ItemsSource = Resourcelist;


            Operationlist = entity2.MicroHard_Operation.ToList();
            this.comboBox3.ItemsSource = Operationlist;
        }

     

      
        private void UpdateBtn_Click(object sender, RoutedEventArgs e)
        {
            FlowChartViewModel vm=(FlowChartViewModel)this.DataContext;
               try
            {
                WorkflowDBDataContext entity = new WorkflowDBDataContext();
                var p = entity.t_FlowCharts.Where(n => n.FlowChartId == vm.SelItem.FlowChartID).SingleOrDefault();
                p.FlowChartName = this.FlowNameTxt.Text;
                p.TypeId = new Guid(this.comboBox1.SelectedValue.ToString());
                p.ResourceID = new Guid(this.comboBox2.SelectedValue.ToString());
                var resource = Resourcelist.Where(n => n.ResourceID == p.ResourceID).SingleOrDefault();
               p.ResourceName = resource.Name;
               p.ResourceKeyName = resource.KeyName;

                p.OperationID = new Guid(this.comboBox3.SelectedValue.ToString());
                var operation = Operationlist.Where(n => n.OperationID == p.OperationID).SingleOrDefault();
               p.OperationName = operation.Name;
              p.OperationKeyName = operation.KeyName;

                entity.SubmitChanges();
              }
            catch
            {
                MessageBox.Show("修改失败！", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                this.Close();
            }
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

      

        }

     
    
}
